package net.huashitong.supervision.AppNew.appService;

import com.sirdc.modules.utils.NumberUtils;
import com.sirdc.modules.utils.ObjectUtils;
import com.sirdc.modules.utils.StringUtils;
import net.huashitong.supervision.AppNew.DTO.viewDto.NumCount;
import net.huashitong.supervision.AppNew.dao.AppTaskUnitDao;
import net.huashitong.supervision.task.entity.TaskCategory;
import net.huashitong.supervision.task.service.TaskCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class AppHomeService {

    @Autowired
    private AppTaskUnitDao dao;
    @Autowired
    private TaskCategoryService categoryService;

    /**
     * 派发数，完成数目
     */
    public HashMap<String,Integer> getSendAndFinishNum(){

        List<NumCount> sendAndFinish = dao.getSendAndFinish();
        for (NumCount andFinish : sendAndFinish) {
            System.out.println(andFinish.getNames() + "  : "+andFinish.getNums());
        }

        List<NumCount> collect = sendAndFinish.stream().filter(numCount -> numCount.getNames().equals("4")).collect(Collectors.toList());
        int sum_total=0;
        int finish =0;
         sum_total = sendAndFinish.stream().mapToInt(value -> value.getNums().intValue()).sum();
         finish = collect.stream().mapToInt(value -> value.getNums().intValue()).sum();

        HashMap<String,Integer>
                map = new HashMap<>();
        map.put("total",sum_total);
        map.put("finish",finish);
//        System.out.println(sendAndFinish);

        return map;

    }

    /**
     * 事项类型
     * @return
     */
    public List<NumCount> getcategoryNums()
    {
        List<NumCount> categoryNum = dao.getCategoryNum();
        int sum = categoryNum.stream().mapToInt(value -> value.getNums().intValue()).sum();

        for (NumCount item : categoryNum) {

            if(StringUtils.isBlank(item.getNames()))
            {
                item.setCodeTexts("未设置类型");
            }else if (("gongjianTask").equals(item.getNames()))
            {
                item.setCodeTexts("攻坚任务");
            }else {
                System.out.println(item.getNames());
                TaskCategory cate = categoryService.getById(item.getNames());
                if(ObjectUtils.isNotBlank(cate))
                {
                    item.setCodeTexts(cate.getCategory());
                }else {
                    item.setCodeTexts("-");
                }
            }
            double rate =(double)item.getNums().intValue()/(sum*1.0) ;
            rate=rate*100;
            String strRate = NumberUtils.keepPrecision(rate, 2);

            item.setRate(Double.parseDouble(strRate));

        }

        return categoryNum;

    }


    public HashMap<String, Integer> getWeekUnitTask(){

        //获取本周第一天；
        Calendar cal=Calendar.getInstance();
        cal.add(Calendar.WEEK_OF_MONTH, -1);
        cal.set(Calendar.DAY_OF_WEEK, 2);
        Date time=cal.getTime();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String fitrstDayOfWeek = sdf.format(time);

        System.out.println(fitrstDayOfWeek);
        List<NumCount> weekUnitNum = dao.getWeekUnitNum(fitrstDayOfWeek);
        System.out.println(weekUnitNum);

        int total=0;
        int finish=0;
        total = weekUnitNum.stream().mapToInt(value -> value.getNums().intValue()).sum();
        List<NumCount> collect = weekUnitNum.stream().filter(numCount -> numCount.getNames().equals("2")).collect(Collectors.toList());
        if(ObjectUtils.isNotBlank(collect))
        {
            NumCount numCount = collect.get(0);
            finish=numCount.getNums().intValue();
        }


        HashMap<String,Integer> map = new HashMap<>();
        map.put("total",total);
        map.put("finish",finish);
        return map;
    }



}
